package com.rslai.base.tool.servertest.command;

import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.exception.ExecuteException;
import com.rslai.base.tool.servertest.model.KeyValueStore;
import com.rslai.base.tool.servertest.response.Response;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;

@Deprecated
public class DbAssertStepCommand extends StepCommand {
    String sql;
    String expect;

    public DbAssertStepCommand(String sql, String expect) {
        this.sql = sql;
        this.expect = expect;
    }

    public Response doExecute(Response preResult, Context context) throws Throwable {
        try {
            this.logger.info("db assert command<sql={}, expect={}> is starting...", this.sql, this.expect);
            List params = Arrays.asList(new KeyValueStore[]{new KeyValueStore("sql", this.sql), new KeyValueStore("database", "default")});
            SqlStepCommand sqlStepCommand = new SqlStepCommand(params);
            Response response = sqlStepCommand.doExecute(preResult, null);
            AssertStepCommand assertStepCommand = new AssertStepCommand(Arrays.asList(new KeyValueStore[]{new KeyValueStore("body", this.expect)}), "");
            assertStepCommand.doExecute(response, context);
            this.logger.info("db assert command<sql={}, expect={}> is finished, result={}", new Object[]{this.sql, this.expect, response.getBody().toString()});
            return preResult;
        } catch (Exception e) {
            String message = String.format("db assert step command invoke error,sql=<%s>, expect=<%s>", new Object[]{this.sql, this.expect});
            this.logger.error(message, e);
            throw new ExecuteException(message, e);
        }
    }

    public StepCommand doClone() {
        return new DbAssertStepCommand(this.sql, this.expect);
    }

    public Map<String, Object> toReport() {
        Map details = new HashMap();
        details.put("stepName", "执行:");
        details.put("name", "数据库验证(该命令已不建议使用，请使用sql和assert代替)");
        List params = new ArrayList();
        params.add(new KeyValueStore("SQL", this.sql));
        params.add(new KeyValueStore("期望", this.expect));
        details.put("params", params);
        return details;
    }
}
